/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.image;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

/**
 *
 * @author HY
 */
public class ImageCacheLoader
{
    public static final String DEFAULT_CACHE_LOCATION = "G:/kurvycache/test";
    public static final double MINIMUM_LENGTH = 480;

    private static BufferedImage bufferImage( Image img )
    {
        BufferedImage result = null;
        if( img != null )
        {
            result = new BufferedImage( img.getWidth( null ), img.getHeight( null ), BufferedImage.TYPE_INT_RGB );
            Graphics2D g2 = result.createGraphics();
            g2.drawImage( img, 0, 0, null );
            g2.dispose();
        }
        return result;
    }

    private static BufferedImage bufferImage( Image img, double rotation )
    {
        BufferedImage result = null;
        if( img != null )
        {
            result = new BufferedImage( img.getWidth( null ), img.getHeight( null ), BufferedImage.TYPE_INT_RGB );
            Graphics2D g2 = result.createGraphics();
            g2.drawImage( img, 0, 0, null );
            g2.dispose();
        }
        return result;
    }


    public static BufferedImage loadImage( URL url ) throws IOException
    {
        BufferedImage result = null;
        if( url != null )
        {
            String path = url.getPath();
            String fullFilename = url.getPath().substring(  path.lastIndexOf( '/' ) );
            String filename = ( fullFilename.lastIndexOf( '.' ) >= 0 )? fullFilename.substring( 0, fullFilename.lastIndexOf( '.' ) ): fullFilename;
            String extension = ( fullFilename.lastIndexOf( '.' ) >= 0 )? fullFilename.substring( fullFilename.lastIndexOf( '.' ) + 1 ):"";

            BufferedImage bufferedImage = bufferImage( ImageIO.read( url ) );
            if( bufferedImage != null )
            {
                File cacheLocation = new File( DEFAULT_CACHE_LOCATION );
                if( cacheLocation.isDirectory() )
                {
                    File cacheImageFile = new File( DEFAULT_CACHE_LOCATION + "/" + filename + "_c." + extension );
                    cacheImageFile.createNewFile();

                    double aspectRatio = (double) bufferedImage.getWidth()/ (double) bufferedImage.getHeight();
                    double width = bufferedImage.getWidth();
                    double height = bufferedImage.getHeight();

                    if( width > MINIMUM_LENGTH || height > MINIMUM_LENGTH )
                    {
                        if( width > height )
                        {
                            width = MINIMUM_LENGTH;
                            height = width / aspectRatio;
                        }
                        else
                        {
                            height = MINIMUM_LENGTH;
                            width = height * aspectRatio;
                        }

                        System.out.println( "Aspect Ratio : " + aspectRatio );
                        System.out.println( "Image Size : " + width + " x " + height );
                        result = bufferImage( bufferedImage.getScaledInstance( (int) width, (int) height, BufferedImage.SCALE_SMOOTH ) );
                        System.out.println( "Scaled Image Size : " + result.getWidth() + " x " + result.getHeight() );
                    }
                    else
                    {
                        result = bufferedImage;
                    }
                }
            }
        }
        return result;
    }
}
